package JavaBeans;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class DBConnection {
	private static String driver ;
	private static String url ;
	private static String user ;
	private static String password ;
	private static Properties pr=new Properties();
	public DBConnection() {
	}
	static { 
		   try {pr.load(DBConnection.class.getClassLoader().getResourceAsStream("db.properties"));
			  driver=pr.getProperty("driver");
			  url=pr.getProperty("url");
			  user=pr.getProperty("username");
			  password=pr.getProperty("password");
			  Class.forName(driver);
			} catch (Exception e) {
				throw new ExceptionInInitializerError(e);
			}
		}
	public static Connection getConnnection() throws SQLException {

		return DriverManager.getConnection(url,user,password);
	}

	public static void free(ResultSet rs, PreparedStatement ps, Connection conn) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (ps != null) {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

